package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.TbActivity;
import com.hqyj.dao.TbActivityMapper;
import com.hqyj.pojo.TbComplaint;
import com.hqyj.service.TbActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author gp
 * @since 2021-09-25
 */
@Service
public class TbActivityServiceImpl implements TbActivityService {

    @Autowired(required = false)
    TbActivityMapper tbActivityMapper;

    @Override
    public HashMap<String, Object> add(TbActivity tbActivity) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = tbActivityMapper.insert(tbActivity);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(TbActivity tbActivity) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = tbActivityMapper.updateById(tbActivity);
        if (num > 0) {
            map.put("info", "保存成功");
        } else {
            map.put("info", "新增失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = tbActivityMapper.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> list(TbActivity tbActivity) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //第一步：创建分页对象
        Page<TbActivity> tbActivityPage = new Page<>(tbActivity.getPage(), tbActivity.getRow());

        //创建查询条件对象
        QueryWrapper<TbActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(
                tbActivity.getCommunityName() != null && !tbActivity.getCommunityName().equals(""),
                "community_name",
                tbActivity.getCommunityName());

        //第二步：查询分页，获取分页数据集
        Page<TbActivity> pageList = tbActivityMapper.selectPage(tbActivityPage, queryWrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> tbActivityByTitle(String title) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<TbActivity> tbActivityList = tbActivityMapper.tbActivityByTitle(title);
        map.put("info", tbActivityList);
        return map;
    }
}
